package com.cby.leetcode.滑动窗口.LC3;

import java.util.HashSet;
import java.util.Set;

/**
 * ClassName: LC3
 * Package: com.cby.leetcode.滑动窗口.LC3
 * Description:
 *
 * @Author dhu-bion
 * @Create 2025/9/10 16:49
 * @Version 1.0
 */
public class LC3 {
    public static void main(String[] args){
        String s = "abcabcbb";
        Solution solution = new Solution();
        int res = solution.lengthOfLongestSubstring(s);
        System.out.println(res);
    }

    static class Solution{
        public int lengthOfLongestSubstring(String s){
            int left = 0;
            int res = 0;
            Set<Character> set = new HashSet<>();

            //1.移动右，扩大窗口
            for (int right = 0; right < s.length(); right++) {
                //获取当前右端点char
                char currentChar = s.charAt(right);

                //判重
                while (set.contains(currentChar)) {
                    //1.有重复，移动左，减小窗口
                    set.remove(s.charAt(left));
                    left ++;
                }

                //当前没有重复字符
                set.add(currentChar);
                res = Math.max(res, right - left + 1);

            }

            return res;
        }

    }
}
